package com.jam.cloud.order.config;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.*;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

//https://www.cnblogs.com/zhxdick/p/14823256.html
//@Configuration 这里不需要添加注解  LoadBalancerClient里已经显示的指定了类
public class LoadBalancerConfig {

//    @Bean
//    public ReactorServiceInstanceLoadBalancer customLoadBalancer(Environment environment, ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider) {
//        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
////        return new RoundRobinLoadBalancer(serviceInstanceListSupplierProvider,name);
//        return new RandomLoadBalancer(serviceInstanceListSupplierProvider,name);
//    }

    //ReactorServiceInstanceLoadBalancer （https://www.modb.pro/db/230052）
    // 与  ReactorLoadBalancer 什么区别

    @Bean
    ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
                                                            LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
//        return new RoundRobinLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }

}
